import {
  Message
} from 'element-ui'
import { debounce } from 'throttle-debounce'
import qs from 'qs'

export const qsparse = (params) => {
  return qs.parse(params)
}
export const errorMessage = debounce(500, (message, type = 'error') => {
  Message({
    message: message,
    duration: 1500,
    type: type
  })
})
export const getNowTime = () => {
  const nowDate = new Date()
  const date = {
    year: nowDate.getFullYear(),
    month: nowDate.getMonth() + 1,
    date: nowDate.getDate(),
    seconds: nowDate.getSeconds(),
    hour: nowDate.getHours(),
    minute: nowDate.getMinutes()
  }
  const newmonth = date.month > 10 ? date.month : '0' + date.month
  const day = date.date > 10 ? date.date : '0' + date.date
  const hour = date.hour < 10 ? '0' + date.hour : date.hour
  const minute = date.minute < 10 ? '0' + date.minute : date.minute
  return date.year + '-' + newmonth + '-' + day + ' ' + hour + ':' + minute + ':' + date.seconds
}
export const response = (data, code, message) => {
  const res = {}
  res.code = code || 0
  res.message = message || ''
  res.data = data
  return res
}
export const result = (mockData) => {
  const res = {}
  res.code = 0
  res.message = ''
  if (typeof (mockData) === 'function') {
    res.data = mockData()
  } else {
    res.data = mockData
  }
  return res
}

export const pageListRes = (dataList, total) => {
  const data = {}
  data.list = dataList || []
  if (total) {
    data.totalRow = total
  } else {
    data.totalRow = dataList ? dataList.length : 0
  }
  return data
}

export const mockPageRes = (params, dataList) => {
  const currPage = params.page
  let limit = params.limit
  let start = (currPage - 1) * limit
  const list = []
  for (let index = 0; index < limit; index++) {
    let v = dataList[start]
    if (v === undefined) {
      break
    }
    list.push(v)
    start++
  }
  return pageListRes(list, dataList.length)
}
